package edu.zzuli.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 
 * @author tianshaojie
 * 
 */
public class StringUtils {
	
	/**
s	 * @author tianshaojie
	 * @date 2010-11-11
	 * @discription : 为空返回true
	 * @param str
	 * @return boolean
	 */
	public static boolean isEmpty(String str) {
		if (str == null || "".equals(str)) {
			return true;
		} else {
			return false;
		}
	}
	
	public static String toString(String[] strArray, String separator) {
		String strResult = "";
		if (strArray == null || separator == null) {
			strResult = null;
		} else {
			StringBuffer strBuffer = new StringBuffer();
			for (int i = 0; i < strArray.length; i++) {
				if (strArray[i] == null || strArray[i].length() == 0)
					continue;
				strBuffer.append(separator);
				strBuffer.append(strArray[i]);
			}
			if (strBuffer.length() > 0) {
				strBuffer.delete(0, separator.length());
			}
			strResult = strBuffer.toString();
		}
		return strResult;
	}

	/**
	 * 数字转中文
	 * 
	 * @param a
	 *            原始数字
	 * @return String 中文字符串
	 */
	public static String translateNumToChinese(int a) {
		String[] units = { "", "十", "百", "千", "万", "十", "百", "千", "亿" };
		String[] nums = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" };
		String result = "";
		if (a < 0) {
			result = "负";
			a = Math.abs(a);
		}
		String t = String.valueOf(a);
		for (int i = t.length() - 1; i >= 0; i--) {
			int r = (int) (a / Math.pow(10, i));
			if (r % 10 != 0) {
				String s = String.valueOf(r);
				String l = s.substring(s.length() - 1, s.length());
				result += nums[Integer.parseInt(l) - 1];
				result += (units[i]);
			} else {
				if (!result.endsWith("零")) {
					result += "零";
				}
			}
		}
		return result;
	}

	public static String convertStr(Object str) {
		if (str == null || str.toString().equals(""))
			return null;
		return str.toString();
	}

	public static Integer convertInt(Object str) {
		if (str == null || "".equals(str.toString()))
			return null;
		return Integer.valueOf(str.toString());
	}

	public static Long convertLong(Object str) {
		if (str == null || "".equals(str.toString()))
			return null;
		return Long.valueOf(str.toString());
	}

	public static Double convertDouble(Object obj) {
		if (obj == null || "".equals(obj.toString()))
			return null;
		return Double.valueOf(obj.toString());
	}
	
	public static BigDecimal convertBigDecimal(Object obj){
		if (obj == null || "".equals(obj.toString()))  
			return null;
		return BigDecimal.valueOf(Double.valueOf(obj.toString()));
	}

	public static String join(Object[] src, String separator, String quot, String defaultValue) {
		StringBuffer sb = new StringBuffer();
		if (src == null || src.length == 0) {
			return defaultValue;
		} else {
			for (int i = 0; i < src.length; i++) {
				if (sb.length() > 0) {
					sb.append(separator);
				}
				if (quot != null) {
					sb.append(quot);
				}
				sb.append(src[i]);
				if (quot != null) {
					sb.append(quot);
				}
			}
			return sb.toString();
		}
	}

	public static String join(List<String> list, String separator, String quot, String defaultValue) {
		StringBuffer sb = new StringBuffer();
		if (list == null || list.size() == 0) {
			return defaultValue;
		} else {
			for (String value : list) {
				if (sb.length() > 0 && separator != null) {
					sb.append(separator);
				}
				if (quot != null) {
					sb.append(quot);
				}
				sb.append(value);
				if (quot != null) {
					sb.append(quot);
				}
			}
			return sb.toString();
		}
	}

	public static String format(String pattern, Object value) {
		if (value == null) {
			if(pattern!=null&&pattern.indexOf("#,##0.00")!=-1){
				return new DecimalFormat(pattern).format(0);
			}
			return "";
		} else if (value instanceof Number) {
			return new DecimalFormat(pattern).format(value);
		} else if (value instanceof Date) {
			return new SimpleDateFormat(pattern).format(value);
		} else if (value instanceof Calendar) {
			return new SimpleDateFormat(pattern).format(((Calendar) value).getTime());
		} else {
			return value.toString();
		}
	}
	
	/**
	 * 作者：程伟平	创建时间：Sep 25, 2008 <p>	
	 * 
	 * 将数字转换为百分比
	 * 
	 * @param num 数字
	 * @return 百分比
	 */
	public static String numToPercent(String num){
		if (num == null) {
			return "";
		} else {
			return num + "%";
		}
	}
	
	/**
	 * 作者：程伟平	创建时间：Nov 4, 2008 <p>	
	 * 
	 * 将字符串转换为LABLE（结尾添加冒号）
	 * 
	 * @param str
	 * @return lable
	 */
	public static String strToLable(String str){
		if (str == null) {
			return "";
		} else {
			return str + "：";
		}
	}

	/**
	 * Java string to JavaScript string. (\n -> \\n) and (" -> ')
	 */
	public static String toJavaScriptString(String s) {
		StringBuffer sb = new StringBuffer(s);
		for (int i = 0; i < sb.length(); i++) {
			char c = sb.charAt(i);
			if (c == '\n') {
				sb.replace(i, i + 1, "\\n");
				i++;
			} else if (c == '"') {
				sb.replace(i, i + 1, "'");
			}
		}
		return sb.toString();
	}
	
	/** 
	 *@author 李智慧
	 *@date 2008-6-6
	 *@discription : 表单对象textarea内容的格式控制（回车、换行、空格）
	 *@param s
	 *@return String
	 */
	public static String textarea2htmlString(String s) {
		if (s != null) {
			return s.replaceAll("\n", "<p>").replaceAll(" ", "&nbsp;");
		}
		return null;
	}

	/**
	 * 生成序列A、B、C...Z、AA、AB...AZ、...
	 */
	public static String int2Excel(int i) {
		String s = "";
		int m = i % 26;
		i = i / 26;
		while (i != 0 || m != 0) {
			if (m == 0) {
				i--;
				m = 26;
			}
			s = (char) (m + (int) ('A') - 1) + s;
			m = i % 26;
			i = i / 26;
		}
		return s;
	}

	/**
	 * 截取字符串，如果字符串长度小于要求的最大长度直接返回，否则截取相应的长度
	 * 
	 * @param str
	 *            要截取的字符串
	 * @param maxLength
	 *            要控制的字符串最大长度;
	 *            注意：如果字符串中包含中文的话，一个汉字按一个长度（不按两个算），也就是说maxLength是中文字符长度，使用该方法的地方就是为了达到该目的而使用的，请不要改动
	 * @return
	 */
	public static String subMaxStr(String str, int maxLength) {
		String result = null;
		if (str != null) {
			if (str.length() <= maxLength) {
				result = str;
			} else {
				result = str.substring(0, maxLength);
			}
		}
		return result;
	}
	
	/**
	 * 截取字符串，如果字符串长度小于要求的最大长度直接返回，否则截取相应的长度,剩余的又......代替，或者由flag里面的表示
	 *@author 易保茹
	 *@date Mar 9, 2009
	 *@discription : 
	 *@param str 传入的字符串
	 *@param maxLength 要截取的最大长度
	 *@param flag 要代替的符号
	 *@return String
	 */
	public static String subMaxStrToEtc(String str,int maxLength,String flag){
		String result = null;
		if (str != null) {
			if (str.length() <= maxLength) {
				result = str;
			} else {
				result = str.substring(0, maxLength)+flag;
			}
		}
		return result;
	}

	/**
	 * 去掉字符串中的回车换行符
	 * 
	 * @param str
	 * @return
	 */
	public static String removeNewline(String str) {
		String result = null;
		if (str != null) {
			result = str.replaceAll("\\r\\n", "");
			result = result.replaceAll(new String(new char[]{10}), "");
		}
		return result;
	}

	public static Integer[] convertStringArray2IntegerArray(String[] values){
		List <Integer> list = new ArrayList<Integer>();
		if (values != null ) {
			for(String value : values) {
				list.add(Integer.valueOf(value));
			}
			return list.toArray(new Integer[]{});
		} else {
			return null;
		}
	}
	
	/** 
	 *@ 任海斌  （专家语音）
	 *@description : 
	 *@param strNum
	 *@return String
	 */
	public static String number2Cn(String strNum) {
		String result = null;
		if(strNum!=null&&strNum.length()>0) {
			result = strNum;
			result = result.replaceAll("0", "零");
			result = result.replaceAll("1", "一");
			result = result.replaceAll("2", "二");
			result = result.replaceAll("3", "三");
			result = result.replaceAll("4", "四");
			result = result.replaceAll("5", "五");
			result = result.replaceAll("6", "六");
			result = result.replaceAll("7", "七");
			result = result.replaceAll("8", "八");
			result = result.replaceAll("9", "九");
		}
		return result;
	}
	
	/** 
	 *@ 任海斌  （专家语音）
	 *@description : 
	 *@param DH
	 *@return String
	 */
	public static String getAWXX(String DH){

			DH = "***********";

		return DH;
	}
	/**
	  *@author  陈伟
	  *@date  Oct 19, 2009 2:57:03 PM 
	  *@description 返回中文数字
	  *@return 
	  */
	public static String LowerToUpperOfNum(int je) {
		if(je == 0) {
			// 2009年4月21日 12:45:37 刘海丹改动
			return "零";
		}
		String money = "";// 转换后的字符串
		String num = "零一二三四五六七八九十";//"零壹贰叁肆伍陆柒捌玖";
		String[] unit = { "", "十", "百", "千", "万", "十万", "百万", "千万", "亿", "十亿", "百亿", "千亿" };
		String beforeDot = String.valueOf(je);// 将金额转换为字符串
		int bl = beforeDot.length();
		boolean zero = false;// 数字是否为零
		int z = 0;// '0'的个数
		// 逐位取数字
		for (int j = 0, i = bl - 1; j <= bl - 1; j++, i--) {
			int number = Integer.parseInt(String.valueOf(beforeDot.charAt(j)));
			if (number == 0) {
				zero = true;
				z++;
			} else {
				zero = false;
				z = 0;
			}
			if (zero && z == 1) {
				money += "零";
			} else if (zero) {
			} else if (!zero) {
				money += num.substring(number, number + 1) + unit[i];
			}
		}
		// 删去多余的'万'和'亿'
		for (int i = 1; i <= 2; i++) {
			String ss = "";
			if (i == 1)
				ss = "万";
			else
				ss = "亿";
			int last = money.lastIndexOf(ss);
			if (last != -1) {
				String moneySub1 = money.substring(0, last);
				String moneySub2 = money.substring(last, money.length());
				int last2 = moneySub1.indexOf(ss);
				while (last2 != -1) {
					moneySub1 = moneySub1.substring(0, last2) + moneySub1.substring(last2 + 1, moneySub1.length());
					last2 = moneySub1.indexOf(ss);
				}
				money = moneySub1 + moneySub2;
			}
		}
			int zi = money.lastIndexOf("零");
			// 如果最后一位字符为'零',则删除它
			if (zi == money.length() - 1) {
				money = money.substring(0, money.length() - 1);//
			}
			//一十开头的变为十
			int type=money.indexOf("一十");
			if(type==0){
				money=money.substring(1);
			}
		return money;
	}
	public static void main(String[] args) {
		System.out.println(format("￥#,##0.00元", 66778899));
		System.out.println(LowerToUpperOfNum(1300));
	}

	
	/**
	 * 功能描述：取得32位UUID<BR>
	 * @return
	 * 时间：2009-1-5 下午12:53:10<BR>
	 */
	public static String  getUUID() {
		return UUID.randomUUID().toString().replaceAll("-", "");
	}
}
